package com.example.automanagement.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.automanagement.dto.AutoOrderDto;
import com.example.automanagement.dto.RepairOrderDto;
import com.example.automanagement.entity.AutoOrder;
import com.example.automanagement.entity.RepairOrder;
import com.example.automanagement.service.OrderArchiveService;
import com.example.automanagement.util.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class OrderArchiveController {
    @Resource
    private OrderArchiveService orderArchiveService;

    @GetMapping("/autoOrderPageQuery")
    public Result autoPageQuery(){
        List<AutoOrder> orders = orderArchiveService.autoPageList();
        return Result.success(orders);
    }
    
    @GetMapping("/repairOrderQuery")
    public Result repairPageQuery(){
        List<RepairOrder> orders = orderArchiveService.repairPageList();
        return Result.success(orders);
    }
    //根据用车工单id查询工单，返回dto封装类
    @GetMapping("/getAutoOrderById")
    public Result getAutoOrderById(@RequestParam("orderId") String orderId){
        AutoOrder order = orderArchiveService.getAutoOrderById(orderId);
        AutoOrderDto orderDto = BeanUtil.copyProperties(order, AutoOrderDto.class);
        return Result.success(orderDto);
    }
    //根据维修工单id查询工单，返回dto封装类
    @GetMapping("/getRepairOrderById")
    public Result getRepairOrderById(@RequestParam("orderId") String orderId){
        RepairOrder order = orderArchiveService.getRepairOrderById(orderId);
        RepairOrderDto orderDto = BeanUtil.copyProperties(order, RepairOrderDto.class);
        return Result.success(orderDto);
    }
    //TODO 用车人确认流程结束之后 把对应工单状态autoOrderStatus改为已归档 维修工单也做配套
    @PutMapping("/updateAutoOrderStatusDone")
    public Result updateAutoOrderStatusDone(@RequestParam("autoOrderId") String autoOrderId){
        if(autoOrderId == null)
            return Result.error("500","检查输入 ");

        orderArchiveService.updateAutoOrderStatus(autoOrderId);
        return Result.success();
    }
    //根据维修工单ID更改维修工修状态，从“流转中”改为“已归档”
    @PutMapping("/updateRepairOrderStatusDone")
    public Result updateRepairOrderStatusDone(@RequestParam("repairOrderId") String repairOrderId){
        if(repairOrderId == null)
            return Result.error("500","检查输入 ");

        orderArchiveService.updateRepairOrderStatus(repairOrderId);
        return Result.success();
    }
}
